package Dao.Impl;

import Dao.CustomerDAO;
import MyUtil.JDBCUtils;
import pojo.Customer;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class CustomerDAOImpl implements CustomerDAO {
    //变量
    private Connection customerDAOImplConn;
    private String sql;
    private PreparedStatement ps;
    private ResultSet rs;

    //构造方法
    public CustomerDAOImpl() throws Exception {
        //获取数据库连接
        customerDAOImplConn = JDBCUtils.getMyConnection();
    }

    //用户登录
    @Override
    public boolean userLogin(String name, String password) throws Exception {
        //查询SQL
        sql = "select * from customer where name = ? and password = ?";
        ps = customerDAOImplConn.prepareStatement(sql);
        //占位符的赋值
        ps.setString(1,name);
        ps.setString(2,password);
        rs = ps.executeQuery();
        //判断结果
        if(rs.next()){
            return true;
        }
//        //关闭资源
//        JDBCUtils.release(customerDAOImplConn,ps,rs);
        return false;
    }

    //用户注册
    @Override
    public boolean userRegister(String username, String password) throws Exception {
        //增加sql语句
        sql = "insert into customer(name,password) values(?,?)";
        ps = customerDAOImplConn.prepareStatement(sql);
        ps.setString(1,username);
        ps.setString(2,password);
        int count = ps.executeUpdate();
        if (count!=0){
            return true;
        }
        return false;
    }

    //用户名查询
    @Override
    public boolean checkUserNameExist(String username) throws Exception {
        //查询SQL
        sql = "select * from customer where name =?";
        ps = customerDAOImplConn.prepareStatement(sql);
        //占位符的赋值
        ps.setString(1,username);
        rs = ps.executeQuery();
        //判断结果
        if(rs.next()){
            return true;
        }
        return false;
    }

    //查询所有客户
    @Override
    public List<Customer> getAllCustomers() throws SQLException {
        List<Customer> customerList = new ArrayList<>();

        sql = "select * from customer";
        ps = customerDAOImplConn.prepareStatement(sql);
        rs = ps.executeQuery();
        while (rs.next()){
            Customer customer = new Customer();
            customer.setCustomerId(rs.getInt("customer_id"));
            customer.setName(rs.getString("name"));
            customer.setContact(rs.getString("contact"));
            customer.setAddress(rs.getString("address"));
            customer.setRegistrationDate(rs.getDate("registrationDate"));  // 数据库中存储的是日期类型
            customer.setLeftMoney(rs.getInt("left_money"));
            customer.setDriverLicenseNumber(rs.getInt("driver_license_number"));
            customer.setPassword(rs.getString("password"));  // 有密码字段
            customer.setRentCarNumber(rs.getInt("rent_car_number"));
            customerList.add(customer);
        }

        return customerList;
    }

    @Override
    public Customer getCustomerById(int customerId) {
        return null;
    }

    //添加用户
    @Override
    public Boolean addCustomer(Customer customer) throws SQLException {
        String sql = "insert into customer (name, contact, address, registrationDate, left_money, driver_license_number, password,rent_car_number) values (?,?, ?, ?, ?, ?, ?, ?)";
        PreparedStatement ps = customerDAOImplConn.prepareStatement(sql);

        ps.setString(1, customer.getName());
        ps.setString(2, customer.getContact());
        ps.setString(3, customer.getAddress());
        // 对于日期，你需要将日期对象转换为正确的格式
        ps.setDate(4, new java.sql.Date(customer.getRegistrationDate().getTime()));
        ps.setInt(5, customer.getLeftMoney());
        ps.setInt(6, customer.getDriverLicenseNumber());
        ps.setString(7, customer.getPassword());
        ps.setInt(8, customer.getRentCarNumber());

        int count = ps.executeUpdate();
        if (count != 0) {
            return true;
        }
        return false;
    }

    @Override
    public void updateCustomer(Customer customer) {

    }

    //删除顾客
    @Override
    public void deletecustomer(int del_customerid) throws SQLException {
        sql = "DELETE FROM customer WHERE customer_id = ?";
        ps = customerDAOImplConn.prepareStatement(sql);
        ps.setString(1, String.valueOf(del_customerid));
        ps.executeUpdate();
    }

    public List<Customer> getCarByBrand(String searchText) throws SQLException {
        List<Customer> customerList = new ArrayList<>();

        sql = "select * from customer where name like ?";
        ps = customerDAOImplConn.prepareStatement(sql);

        String fuzzyKeyword = "%"+searchText+"%";
        ps.setString(1, fuzzyKeyword);
        rs = ps.executeQuery();

        while (rs.next()){
            Customer customer = new Customer();
            customer.setCustomerId(rs.getInt("customer_id"));
            customer.setName(rs.getString("name"));
            customer.setContact(rs.getString("contact"));
            customer.setAddress(rs.getString("address"));
            customer.setRegistrationDate(rs.getDate("registrationDate"));  // 数据库中存储的是日期类型
            customer.setLeftMoney(rs.getInt("left_money"));
            customer.setDriverLicenseNumber(rs.getInt("driver_license_number"));
            customer.setPassword(rs.getString("password"));  // 有密码字段
            customer.setRentCarNumber(rs.getInt("rent_car_number"));
            customerList.add(customer);
        }
        return customerList;
    }
}
